INCLUDE(InstallPackageCommon)

FIND_PROGRAM(MAKE_EXECUTABLE "make")
IF(NOT MAKE_EXECUTABLE)
    MESSAGE(FATAL_ERROR "make: command not found.")
ENDIF()

FIND_PROGRAM(CMAKE_EXECUTABLE "cmake")
IF(NOT CMAKE_EXECUTABLE)
    MESSAGE(FATAL_ERROR "cmake: command not found.")
ENDIF()

FIND_PROGRAM(SH_EXECUTABLE "sh")
IF(NOT CMAKE_EXECUTABLE)
    MESSAGE(FATAL_ERROR "sh: command not found.")
ENDIF()


MACRO(MINGW_BUILD_ENVIRONMENT)
	SET(MINGW_BUILD_ENV ${CMAKE_BINARY_DIR}/mingw-libs)
	
	FILE(MAKE_DIRECTORY ${MINGW_BUILD_ENV})
    FILE(MAKE_DIRECTORY ${MINGW_BUILD_ENV}/tmp)
    FILE(MAKE_DIRECTORY ${MINGW_BUILD_ENV}/src)
	FILE(MAKE_DIRECTORY ${MINGW_BUILD_ENV}/pkg)
	
	SET(ZLIB_PACKAGE_DIR "${MINGW_BUILD_ENV}/pkg/zlib")
	IF(NOT EXISTS ${ZLIB_PACKAGE_DIR})
		CE_UNPACK_PACKAGE(${MINGW_BUILD_ENV}
						  "zlib"
						  "http://zlib.net/zlib-1.2.8.tar.gz"
						  "${MINGW_BUILD_ENV}/src"
						  _PACKAGE_RESULT)
		MESSAGE("\n**************************************************************\n"
				"Building zlib\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${MAKE_EXECUTABLE}" "-f" "win32/Makefile.gcc"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE BUILD_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(BUILD_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while building the following package: zlib.\n"
								"${ERROR_RESULT}.")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Installing zlib\n"
				"**************************************************************\n\n")
		SET(ENV{BINARY_PATH} ${ZLIB_PACKAGE_DIR}/bin)
		SET(ENV{INCLUDE_PATH} ${ZLIB_PACKAGE_DIR}/include)
		SET(ENV{LIBRARY_PATH} ${ZLIB_PACKAGE_DIR}/lib)
		EXECUTE_PROCESS(COMMAND "${MAKE_EXECUTABLE}" "-f" "win32/Makefile.gcc" "install"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE INSTALL_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(INSTALL_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while installing the following package: zlib.\n"
								"${ERROR_RESULT}.")
			FILE(REMOVE_RECURSE ${ZLIB_PACKAGE_DIR})
		ENDIF()
	ENDIF()
	SET(ZLIB_ROOT ${ZLIB_PACKAGE_DIR})
	
	SET(LIBPNG_PACKAGE_DIR "${MINGW_BUILD_ENV}/pkg/libpng")
	IF(NOT EXISTS ${LIBPNG_PACKAGE_DIR})
		CE_UNPACK_PACKAGE(${MINGW_BUILD_ENV}
						  "libpng"
						  "ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.6.tar.gz"
						  "${MINGW_BUILD_ENV}/src"
						  _PACKAGE_RESULT)
		MESSAGE("\n**************************************************************\n"
				"Configuring libpng\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${CMAKE_EXECUTABLE}" "-G" "MSYS Makefiles" "." "-DCMAKE_INSTALL_PREFIX=${LIBPNG_PACKAGE_DIR}"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE BUILD_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(BUILD_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while building the following package: libpng.\n"
								"${ERROR_RESULT}.")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Building libpng\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${MAKE_EXECUTABLE}"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE BUILD_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(BUILD_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while building the following package: libpng.\n"
								"${ERROR_RESULT}.")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Installing libpng\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${MAKE_EXECUTABLE}" "install"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE INSTALL_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(INSTALL_RESULT)
			MESSAGE("an error has occurred while building the following package: libpng.\n"
			        "${ERROR_RESULT}.\n"
			        "We are going to try to install it by hand because the build scripts are broken.")
			SET(LIBPNG_INCLUDES
				${MINGW_BUILD_ENV}/src/libpng/png.h
				${MINGW_BUILD_ENV}/src/libpng/pngconf.h
				${MINGW_BUILD_ENV}/src/libpng/pnglibconf.h)
			FILE(INSTALL ${LIBPNG_INCLUDES} DESTINATION "${MINGW_BUILD_ENV}/pkg/libpng/include")
		ENDIF()
	ENDIF()
	SET(PNG_LIBRARY ${LIBPNG_PACKAGE_DIR}/lib/libpng16.a)
	SET(PNG_PNG_INCLUDE_DIR ${LIBPNG_PACKAGE_DIR}/include)
	
	SET(LIBFREETYPE_PACKAGE_DIR "${MINGW_BUILD_ENV}/pkg/libfreetype")
	IF(NOT EXISTS ${LIBFREETYPE_PACKAGE_DIR})
		CE_UNPACK_PACKAGE(${MINGW_BUILD_ENV}
					  "libfreetype"
					  "http://download.savannah.gnu.org/releases/freetype/freetype-2.5.0.1.tar.gz"
					  "${MINGW_BUILD_ENV}/src"
					  _PACKAGE_RESULT)
		MESSAGE("\n**************************************************************\n"
				"Configuring libfreetype\n"
				"**************************************************************\n\n")
		STRING(REGEX REPLACE "([A-Za-z]):(.*)" "/\\1\\2" LIBPNG_PATH ${LIBPNG_PACKAGE_DIR})
		STRING(REGEX REPLACE "([A-Za-z]):(.*)" "/\\1\\2" LIBFREETYPE_PATH ${LIBFREETYPE_PACKAGE_DIR})
		# Some hacks were added to make to compile
		SET(ENV{LIBPNG_CFLAGS} "-I${LIBPNG_PATH}/include -Doff64_t=_off64_t -Doff_t=_off_t")
		SET(ENV{LIBPNG_LDFLAGS} "-L${LIBPNG_PATH}/lib -lpng16")
		EXECUTE_PROCESS(COMMAND "${SH_EXECUTABLE}" "./configure" "--prefix=${LIBFREETYPE_PATH}"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE CONFIGURE_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(CONFIGURE_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while configuring the following package: libfreetype.\n"
								"${ERROR_RESULT}")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Building libfreetype\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND ${MAKE_EXECUTABLE}
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE BUILD_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(BUILD_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while building the following package: libfreetype.\n"
								"${ERROR_RESULT}.")
		ENDIF()
		MESSAGE("\n**************************************************************\n"
				"Installing libfreetype\n"
				"**************************************************************\n\n")
		EXECUTE_PROCESS(COMMAND "${MAKE_EXECUTABLE}" "install"
						WORKING_DIRECTORY ${_PACKAGE_RESULT}
						RESULT_VARIABLE INSTALL_RESULT
						ERROR_VARIABLE ERROR_RESULT)
		IF(INSTALL_RESULT)
			MESSAGE(FATAL_ERROR "an error has occurred while installing the following package: libfreetype.\n"
								"${ERROR_RESULT}.")
			FILE(REMOVE_RECURSE ${LIBFREETYPE_PACKAGE_DIR})
		ENDIF()
	ENDIF()
	SET(FREETYPE_LIBRARY ${LIBFREETYPE_PACKAGE_DIR}/lib/libfreetype.a)
	SET(FREETYPE_INCLUDE_DIRS ${LIBFREETYPE_PACKAGE_DIR}/include/freetype2)
	
ENDMACRO(MINGW_BUILD_ENVIRONMENT)